#pragma once
#include "Protocol.hpp"

enum
{
    DIV_ERR,
    MOD_ERR,
    Other_ERR
};
class Calculator
{
public:
    Calculator()
    {
    }
    Response CalculatorHandler(const Request &rq)
    {
        Response rp(0, 0);
        switch (rq.op)
        {
        case '+':
            rp.result_ = rq.x + rq.y;
            break;
        case '-':
            rp.result_ = rq.x - rq.y;
            break;
        case '*':
            rp.result_ = rq.x * rq.y;
            break;
        case '/':
        {
            if (rq.y == 0)
                rp.code_ = DIV_ERR;
            else
                rp.result_ = rq.x / rq.y;
        }
        break;
        case '%':
        {
            if (rq.y == 0)
                rp.code_ = MOD_ERR;
            else
                rp.result_ = rq.x % rq.y;
        }
        break;
        default:
            Other_ERR;
            break;
        }
        return rp;
    }

    std::string Handler(std::string package)
    {
        std::string content;
        bool r=Decode(&content,package);
        if(!r)
            return "";
        Request rq;
        r=rq.Deserialize(content); 
        if(!r)
            return "";
        Response rp=CalculatorHandler(rq);
        rp.Serialize(&content);
        content=Encode(content);
        return content;
    }
    ~Calculator()
    {
    }
};